﻿using System;
using System.Drawing;
using System.Web;
using System.Web.UI;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument
{
    /// <summary>
    /// oClassNode 的摘要说明。
    /// </summary>
    public partial class OClassNodePage : Page
    {
        public static string ClassID = "";
        public string Action = "";

        private void Page_Load(object sender, EventArgs e)
        {
            ClassID = (Request.QueryString["ClassID"] != null) ? Request.QueryString["ClassID"] : "";
            Action = (Request.QueryString["Action"] != null) ? Request.QueryString["Action"] : "";
            if (!Page.IsPostBack)
            {
                if (Action == "1")
                {
                    lblCreate.BackColor = Color.FromName("#1ED2CA");
                    btnRevise.Visible = false;
                    btnDelete.Visible = false;
                }
                if (Action == "2")
                {
                    var prj = new ProjectClass();
                    lblDelete.BackColor = Color.FromName("#1ED2CA");
                    btnRevise.Visible = false;
                    btnAdd.Visible = false;
                    btnDelete.Visible = true;
                    PopulateReviseData();

                    if (prj.IsExistSubClass(Int32.Parse(ClassID)))
                        btnDelete.Attributes["onClick"] = "javascript:alert('此项目还存在子节点，不能删除!!');return false;";
                    else
                        btnDelete.Attributes["onClick"] = "javascript:return confirm('您确认吗?');";
                }
                if (Action == "3")
                {
                    lblRevise.BackColor = Color.FromName("#1ED2CA");
                    btnRevise.Visible = true;
                    btnAdd.Visible = false;
                    PopulateReviseData();
                }

                PopulateData();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var cls = new Class();
            String Username = SiteContext.Current.UserName;
            try
            {
                cls.AddClass(Int32.Parse(ClassID), txtAClassName.Text, txtAClassRemark.Text, 1, Username, DateTime.Now, 0);
                Response.Write("<script language=javascript>alert('添加成功!');parent.LeftFrame.location='ProjectTreeView.aspx?classID=" + ClassID + "';</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        private void btnRevise_Click(object sender, EventArgs e)
        {
            var dc = new DirectoryClass();
            try
            {
                dc.Revise(Int32.Parse(ClassID), txtAClassName.Text, txtAClassName.Text, 1, 100, DateTime.Today, DateTime.Today);
                Response.Write("<script language=javascript>alert('修改成功!');parent.LeftFrame.location='ProjectTreeView.aspx?classID=" + ClassID + "';</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DeleteClass();
        }

        #region 删除节点

        /// <summary>
        /// 删除节点
        /// </summary>
        private void DeleteClass()
        {
            var prj = new ProjectClass();
            try
            {
                prj.Delete(Int32.Parse(ClassID));
                Response.Write("<script language=javascript>alert('删除成功!');parent.location.reload();</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        #endregion

        #region 绑定修改时数据

        /// <summary>
        /// 绑定修改时数据
        /// </summary>
        private void PopulateReviseData()
        {
            ClassInfo info = Class.GetClass(Int32.Parse(ClassID));
            txtAClassName.Text = info.Name;
            txtAClassRemark.Text = info.Description;
        }

        #endregion

        #region 绑定初始化时数据

        private void PopulateData()
        {
            lblParentNodeName.Text = Class.GetClassName(Int32.Parse(ClassID));
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            this.btnRevise.Click += new System.EventHandler(this.btnRevise_Click);
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}